package com.study.redis.shoppingcart;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Tuple;

import java.util.Set;

/**
 * Create by zhaihongwei on 2018/2/27
 * 购物车
 */
public class ShoppingCartDemo {

    public static void main(String[] args) {
        Jedis jedis = new Jedis();
        try {
            //产品列表
            String key0="千寻产品:FindS";
            String key1="千寻产品:FindCm";
            String key2="千寻产品:FindM";
            String key3="千寻产品:FindMM";
            String key4="千寻产品:云踪";
            String key5="千寻产品:FindNow";
            final String[] aliyunProducts = new String[] {key0,key1,key2,key3,key4,key5};

            // 初始化，清除可能已有的旧数据
            for (int i = 0 ; i < aliyunProducts.length ; i++) {
                jedis.del(aliyunProducts[i]);
            }

            //模拟用户购物
            for (int i = 0; i < 5; i++) {//模拟多人次的用户购买行为
                customersShopping(aliyunProducts,i,jedis);
            }
            System.out.println();
            //利用ApsaraDB for Redis来输出各个商品间的关联关系
            for (int i = 0; i < aliyunProducts.length; i++) {
                System.out.println(">>>>>>>>>>与"+aliyunProducts[i]+"一起被购买的产品有<<<<<<<<<<<<<<<");
                Set<Tuple> relatedList = jedis.zrevrangeWithScores(aliyunProducts[i], 0, -1);
                for (Tuple item : relatedList) {
                    System.out.println("商品名称："+item.getElement()+"， 共同购买次数:"+Double.valueOf(item.getScore()).intValue());
                }
                System.out.println();
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            jedis.close();
        }
    }

    private static void customersShopping(String[] products, int i, Jedis jedis) {
        //简单模拟3种购买行为，随机选取作为用户的购买选择
        int bought=(int)(Math.random()*3);
        if(bought==1){
            //模拟业务逻辑：用户购买了如下产品
            System.out.println("用户"+i+"购买了"+products[0]+","+products[2]+","+products[1]);
            //将产品之间的关联情况记录到ApsaraDB for Redis的SortSet之中
            jedis.zincrby(products[0], 1, products[1]);
            jedis.zincrby(products[0], 1, products[2]);
            jedis.zincrby(products[1], 1, products[0]);
            jedis.zincrby(products[1], 1, products[2]);
            jedis.zincrby(products[2], 1, products[0]);
            jedis.zincrby(products[2], 1, products[1]);
        }else if(bought==2){
            //模拟业务逻辑：用户购买了如下产品
            System.out.println("用户"+i+"购买了"+products[4]+","+products[2]+","+products[3]);
            //将产品之间的关联情况记录到ApsaraDB for Redis的SortSet之中
            jedis.zincrby(products[4], 1, products[2]);
            jedis.zincrby(products[4], 1, products[3]);
            jedis.zincrby(products[3], 1, products[4]);
            jedis.zincrby(products[3], 1, products[2]);
            jedis.zincrby(products[2], 1, products[4]);
            jedis.zincrby(products[2], 1, products[3]);
        }else if(bought==0){
            //模拟业务逻辑：用户购买了如下产品
            System.out.println("用户"+i+"购买了"+products[1]+","+products[5]);
            //将产品之间的关联情况记录到ApsaraDB for Redis的SortSet之中
            jedis.zincrby(products[5], 1, products[1]);
            jedis.zincrby(products[1], 1, products[5]);
        }
    }
}
